$server = 'http://localhost:8080/';

window.onload = function(){
	// 购物车信息
	$.ajax(
	{
		url: $server+'cart/list.do',
		type: "POST",
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
			if (result.data.length == 0){
				alert('购物车为空');
				window.history.back(-1); 
			}
			// 商品数量
			$('#commodityCount')[0].innerHTML = result.data.commoditySum;
			// 总额
			$('#totalPrice')[0].innerHTML = result.data.cartTotalPrice;
			// 应付
			$('#shouldPrice')[0].innerHTML = result.data.cartTotalPrice;
			
			
			$htmlstr = '';
				
			$.each(result.data.cartCommodityVoList, function(index, value){
				
				$htmlstr += '<div class="address_info1 address_info2">' +
				    '<div class="address_distribution">' +
				        '<div class="address_distribution1">' +
				            '<p>配送方式</p>' +
				            '<div>' +
				                '快速运输' +
				            '</div>' +
				            '<span>配送时间: 工作日、双休日与节假日均可送货</span>' +
				        '</div>' +
// 				        '<div class="address_distribution1_10">' +
// 				            '<span>萨达所大</span>' +
// 				        '</div>' +
// 				        '<div class="address_distribution1_11">' +				
// 				        '</div>' +
// 				        '<div class="address_distribution1_12">' +
// 				            '<span>选择其他商品选择小赠品可选择加购选择其他小商品</span>' +
// 				        '</div>' +
// 				        '<div class="address_distribution1_13">' +
// 				            '<span>￥1.50</span>' +
// 				        '</div>' +
				    '</div>' +
				    '<div class="address_distribution_right">' +
				        '<div>' +
				            '<img src="img/confirm_order/FirmOrder_picture.png" alt="">' +
				        '</div>' +
				        '<div>' +
				            '<p>奥义 瑜伽垫 TPE加宽加大183*80CM</p>' +
				            '<p>男女加厚健身垫 加长无味环保防滑运动</p>' +
				        '</div>' +
				        '<div>' +
				            '<span class="address_distribution2_1">￥'+value.commodityPrice+'</span>' +
				        '</div>' +
				        '<div>' +
				            '<b>x'+value.quantity+'</b>' +
				        '</div>' +
				        '<div>' +
				            '<span>有货</span>' +
				        '</div>' +
				    '</div>' +
				'</div>';
			});
			
			$('#commodityItem')[0].innerHTML = $htmlstr;
		}
	});
		
	// 收货地址信息
	$.ajax(
	{
		url: $server+'shipping/list.do',
		type: "POST",
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			console.log(result);
			if (result.code == 200){				
				if (result.data.list.length == 0){
					alert('请填写收货地址');
					return;
				}
				
				
				$htmlstr = '';
				$.each(result.data.list, function(index, value){		
					$active = '';
					$defualt = '';
					$radio = '';
					if (index == 0){
						$active = 'management_add_send';
						$defualt = '<span>默认收货地址</span>';
						$radio = 'checked';
						
						// 收货地址
						$('#addr')[0].innerHTML = value.receiverCity + ' ' + value.receiverDistrict + ' ' + value.receiverAddress;
						// 联系人
						$('#recive')[0].innerHTML = value.receiverName + ' ' + value.receiverPhone;
					}					
										
					$htmlstr +=  '<li class="Management_address_ul_li '+$active+'" data-shiipingId='+value.id+' >' +
									'<div class="Management_address_ul_li_div">' +
										'<p>寄送至</p>' +
									'</div>' +
									'<input type="radio" name="address" onclick=shippingClick(this) '+$radio+' >' +
									'<span>'+value.receiverCity+' '+value.receiverDistrict+' '+value.receiverAddress+'（'+value.receiverName+' 收）</span>' +
									'<span>'+value.receiverPhone+'</span>' +
									$defualt +
									'<a href="shipping.html?id='+value.id+'">修改本地址</a>' +
								'</li>';
				});
				$('#Management_address_ul')[0].innerHTML = $htmlstr;
			}else{
				alert(result.msg);
			}
		}
	});
}

// 选择收货地址
function shippingClick(obj){
	$(obj).parent().addClass('management_add_send').siblings().removeClass('management_add_send');;
	$(obj).parent().find('p').css({'display':'block'}).end().siblings().find('p').css({'display':'none'});
	$(obj).parent().find('a').css({'display':'block'}).end().siblings().find('a').css({'display':'none'});
}

// 提交订单
function submit(){
	$obj = $('.management_add_send')[0];	
	$shiipingId = $($obj).attr('data-shiipingId');

$.ajax(
	{
		url: $server+'order/create.do',
		type: "POST",
		data: {shippingId: $shiipingId},
		datatype: 'json',		
		crossDomain:true, //设置跨域为true
		xhrFields:{
			withCredentials: true // 携带跨域cookie
		},
		success: function(result){
			if (result.code == 200){
				alert('下单成功,请及时付款');
				$orderNo = result.data.orderNo;
				window.location.href = encodeURI('pay.html?orderNo='+$orderNo);
			}else{
				alert(result.msg);
			}
		}
	});
}